/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.railway.classes.Ticket;
import com.railway.utils.RestServer;
import java.awt.AWTException;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Moofie
 */
public class TicketPrint extends javax.swing.JDialog {
    String[] refNo = Payment.getReferenceNo();
    String[] ticketSeatNo = Payment.getTickets();
    private JDialog dialog1;
    private JDialog dialog2;
    private String code;
    /**
     * Creates new form TicketPrint
     */
    public TicketPrint(java.awt.Frame parent, boolean modal, JDialog dialog1, JDialog dialog2) {
        super(parent, modal);
        initComponents();
        this.dialog1=dialog1;
        this.dialog2=dialog2;
        jpTickets.setLayout(new GridLayout(Ticket.getNoTickets(),1));
        
        
        for(int i = 0; i<Ticket.getNoTickets();i++){
            if(ticketSeatNo[i]!=null){
            String a = RestServer.httpGet("GetTicketCodeByRef", refNo[i]);
            System.out.println(a);
            try {
                JSONObject json = new JSONObject(a);
                JSONArray content = json.getJSONArray("content");
                
                code = content.getJSONObject(0).getString("code");
                ImageIcon icon = getImageFile(code);
                JLabel label = new JLabel("<html>Seat No :"+ticketSeatNo[i]+
                                            "<br />Origin :"+Ticket.getOrigin()+
                                            "<br />Destination :"+Ticket.getDestination()+
                                            "<br />Date :"+Ticket.getBookedDate()+
                                            "<br />Depature :"+Ticket.getDepatureTime()+
                                            "</html>");
                label.setIcon(icon);
                jpTickets.add(label);
            } catch (JSONException ex) {
                System.out.println(ex);
            }
            }
        }
        
    }
    
    private ImageIcon getImageFile(String url) {
        Image img;
        BufferedImage bfImg = null;
        try {
                String serverurl = "http://localhost/RailwayWeb/qrgenerator.php?param=";
                img = new ImageIcon(new URL(serverurl + url)).getImage();

            bfImg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = bfImg.createGraphics();
            g2d.drawImage(img, 0, 0, 200, 200, this);
            g2d.dispose();
        } catch (MalformedURLException ex) {
            Logger.getLogger(PointRedemptionPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return new ImageIcon(bfImg);
    }



    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jpTickets = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jbtClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout jpTicketsLayout = new javax.swing.GroupLayout(jpTickets);
        jpTickets.setLayout(jpTicketsLayout);
        jpTicketsLayout.setHorizontalGroup(
            jpTicketsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );
        jpTicketsLayout.setVerticalGroup(
            jpTicketsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jpTickets);

        jButton1.setText("Print");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jbtClose.setText("Close");
        jbtClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtClose, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jbtClose))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
    final BufferedImage image = new BufferedImage(
    jpTickets.getWidth(), jpTickets.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics gr = image.getGraphics();
    jpTickets.printAll(gr);
    
    
    try{
        Robot robot = new Robot();
        final Rectangle rect = jpTickets.getBounds();
        final BufferedImage images = robot.createScreenCapture(rect);
        ImageIO.write(image, "PNG", new File("C:\\Users\\jason1\\Desktop\\Tickets.png"));
    }catch(IOException ex){
        System.out.println(ex);
    }catch(AWTException ex){
        System.out.println(ex);
    }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jbtCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCloseActionPerformed
        this.setVisible(false);
        dialog1.dispose();
        dialog2.dispose();
    }//GEN-LAST:event_jbtCloseActionPerformed

    /**
     * @param args the command line arguments
     */
//
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtClose;
    private javax.swing.JPanel jpTickets;
    // End of variables declaration//GEN-END:variables
}
